from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from .models import *
from .forms import IpForm,HostForm,HostInfoForm,IdcForm


def index(request):
	'''主页'''
	return render(request,'hostmanage/index.html')


def idcs(request):
	"""展示所有机房"""
	idcs = Idc.objects.all()
	context = {'idcs':idcs}
	return render(request,'hostmanage/idcs.html',context)


def idc(request,idc_id):
	"""展示单个机房信息"""
	idc = Idc.objects.get(id=idc_id)
	hosts = idc.host_set.order_by('-date_added')
	context = {'idc':idc,'hosts':hosts}
	return render(request,'hostmanage/idc.html',context)


def add_idc(request):
	"""添加机房"""
	if request.method != 'POST':
		form = IdcForm()
	else:
		form = IdcForm(request.POST)
		if form.is_valid():
			new_idc = form.save(commit=False)
			new_idc.owner = request.user
			form.save()
			return HttpResponseRedirect(reverse('hostmanage:idcs'))
	context = {'form':form}
	return render(request,'hostmanage/add_idc.html',context)


def edit_idc(request,idc_id):
	"""编辑机房信息"""
	idc = Idc.objects.get(id=idc_id)
	if request.method != 'POST':
		form = IdcForm(instance=idc)
	else:
		form = IdcForm(instance=idc,data=request.POST)
		if form.is_valid():
			form.save()
			return HttpResponseRedirect(reverse('hostmanage:idcs'))
	context = {'idc':idc,'form':form}
	return render(request,'hostmanage/edit_idc.html',context)


def host(request,host_id):
	"""展示主机详细信息"""
	host = Host.objects.get(id=host_id)
	hostinfos = host.hostinfo_set.all()
	hostips = host.ip_set.all()

	
	context = {'host':host,'hostinfos':hostinfos,'hostips':hostips}
	return render(request,'hostmanage/host.html',context)


def add_host(request,idc_id):
	"""添加主机信息"""
	idc = Idc.objects.get(id=idc_id)

	if request.method != 'POST':
		hostform = HostForm()
		hostinfoform = HostInfoForm()
		ipform = IpForm()
	else:
		hostform = HostForm(request.POST)
		hostinfoform = HostInfoForm(request.POST)
		ipform = IpForm(request.POST)
		if hostform.is_valid() and hostinfoform.is_valid() and ipform.is_valid():
			"""先不保存主机数据，关联上外键idc后再保存"""
			add_hostform = hostform.save(commit=False)
			add_hostform.idc = idc
			add_hostform.save()
			"""先不保存主机详细信息数据，关联上外键主机后再保存"""
			add_hostinfoform = hostinfoform.save(commit=False)
			add_hostinfoform.host = add_hostform
			add_hostinfoform.save()
			"""先不保存Ip数据，关联上外键主机后再保存"""
			add_ipform = ipform.save(commit=False)
			add_ipform.host = add_hostform
			add_ipform.save()
			return HttpResponseRedirect(reverse('hostmanage:idc',args=[idc_id]))

	context = {'idc':idc,'hostform':hostform,'hostinfoform':hostinfoform,'ipform':ipform}
	return render(request,'hostmanage/add_host.html',context)


def edit_host(request,host_id):
	"""编辑主机信息"""
	host_data = Host.objects.get(id=host_id)
	hostinfo_data = HostInfo.objects.get(host=host_data)
	ip_data = Ip.objects.get(host=host_data)

	if request.method != 'POST':
		hostform = HostForm(instance=host_data)
		hostinfoform = HostInfoForm(instance=hostinfo_data)
		ipform = IpForm(instance=ip_data)
	else:
		hostform = HostForm(instance=host_data,data=request.POST)
		hostinfoform = HostInfoForm(instance=hostinfo_data,data=request.POST)
		ipform = IpForm(instance=ip_data,data=request.POST)
		if hostform.is_valid() and hostinfoform.is_valid() and ipform.is_valid():
			hostform.save()
			hostinfoform.save()
			ipform.save()
			return HttpResponseRedirect(reverse('hostmanage:host',args=[host_id]))
	context = {'hostform':hostform,'hostinfoform':hostinfoform,'ipform':ipform,'host_data':host_data}
	return render(request,'hostmanage/edit_host.html',context)